<?php


class UploadedFile
{
    public $array = false;
    public $count = 0;

    public $name;
    public $type;
    public $basename;
    public $extension;
    public $size;
    public $tmpname;
    public $error;

    public function __construct($uploadedFile)
    {
        $this->array = is_array($uploadedFile["name"]);

        $this->name = $uploadedFile["name"];
        $this->type = $uploadedFile["type"];
        $this->size = $uploadedFile["size"];
        $this->tmpname = $uploadedFile["tmp_name"];
        $this->error = $uploadedFile["error"];

        if(! $this->array)
        {
            $this->extension = end(explode(".", $this->name));
            $this->basename = basename($this->name,".".$this->extension);
            $this->count++;
        } else
        {
            foreach($this->name as $n)
            {
                $this->extension[] = end(explode(".", $n));
                $this->basename[] = basename($n,".".end($this->extension));
                $this->count++;
            }
        }
    }

    public function moveTo($dir, $newName = null)
    {
        if(! $this->array)
        {
            if($newName == null)
                $fileName = $this->name;
            else
                $fileName = $newName;

            if (move_uploaded_file($this->tmpname,$dir.DIR_SEPARATOR.$fileName))
            {
                return Files::GetFile($dir.DIR_SEPARATOR.$fileName, fcBaseInfo); //$dir.DIR_SEPARATOR.$fileName;
            } else
            {
                $this->error = UPLOAD_ERR_CANT_WRITE;
                return null;
            }

        } else
        {
            $res = array();

            for($i=0; $i < $this->count; $i++)
            {
                if($newName == null)
                    $fileName = $this->basename[$i]."_".$i.".".$this->extension[$i];
                else
                {
                    $e = explode(".", $newName);
                    $tmpext = count($e)==1 ? "" : end($e);
                    $fileName = basename($newName,".".$tmpext)."_".$i.(empty($tmpext) ? "" : ".".$tmpext);
                }

                if(move_uploaded_file($this->tmpname[$i],$dir.DIR_SEPARATOR.$fileName))
                {
                    $res[] = Files::GetFile($dir.DIR_SEPARATOR.$fileName); //$dir.DIR_SEPARATOR.$fileName;
                } else
                {
                    $this->error[$i] = UPLOAD_ERR_CANT_WRITE;
                    $res[] = null;
                }
                
            }

            return $res;
        }
    }

}

class UploadedFiles implements ArrayAccess, Iterator
{
    public $files = array();
    private $position = 0;
    private $keys = null;
    private $keyscount = 0;

    function __construct()
    {
        foreach($_FILES as $field => $file)
        {
            $this->files[$field] = new UploadedFile($file);
        }
    }

    function offsetGet($name)
    {
        return isset($this->files[$name]) ? $this->files[$name] : null;
    }

    function offsetSet($name,$val)
    {
        $this->files[$name] = $val;
    }

    function offsetExists($name)
    {
        return isset($this->files[$name]);
    }

    function offsetUnset($name)
    {
        unset($this->files[$name]);
    }

    function Set($name,$val, $time)
    {
        $this->files[$name] = $val;
    }

    function rewind()
    {
        $this->position = 0;
        if(! $this->keys)
        {
            $this->keys = array_keys($this->files);
            $this->keyscount = count($this->keys);
        }
    }

    function current()
    {
        return $this->files[$this->keys[$this->position]];
    }

    function key()
    {
        return $this->keys[$this->position];
    }

    function next()
    {
        ++$this->position;
    }

    function valid()
    {
        return $this->position < $this->keyscount && isset($this->files[$this->keys[$this->position]]);
    }

    function GetRaw()
    {
        return $_FILES;
    }

};
?>
